/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: AbstractEditView.java
 * Date: 11/02/2011
 */

package com.ingenius.sau.client.view;

import com.extjs.gxt.ui.client.Style.Direction;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.core.XDOM;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ComponentPlugin;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.InfoConfig;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;
import com.ingenius.sau.client.presenter.EditPresenter;
import com.ingenius.sau.client.presenter.Presenter;

/**
 * Class <strong>AbstractEditView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public abstract class AbstractEditView extends Window implements Display {

	protected final FormPanel form = new FormPanel();

	protected EditPresenter presenter = null;

	protected ComponentPlugin plugin;

	public AbstractEditView() {
		setLayout(new FitLayout());
		setModal(true);
		setResizable(false);
		setSize(500, 250);

		// TODO i18n
		final Button saveButton = new Button("Save");
		final Button cancelButton = new Button("Cancel");

		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (isValid()) {
					presenter.doSaveOrUpdate();
				}
			}
		});

		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				hide();
			}
		});

		plugin = new ComponentPlugin() {
			public void init(Component component) {
				component.addListener(Events.Render,
						new Listener<ComponentEvent>() {
							public void handleEvent(ComponentEvent be) {
								El elem = be.getComponent().el()
										.findParent(".x-form-element", 3);
								// should style in external CSS rather than
								// directly
								elem.appendChild(XDOM
										.create("<div style='color: #615f5f;font-size: 8.5pt;padding: 1px 0 2px;text-align: left;'>"
												+ be.getComponent().getData(
														"text") + "</div>"));
							}
						});
			}
		};

		addButton(saveButton);
		addButton(cancelButton);
		setButtonAlign(HorizontalAlignment.CENTER);
		form.setHeaderVisible(false);
	}

	protected boolean isValid() {
		return form.isValid();
	}

	/**
	 * This method should create the graphics components that make up the view
	 * and add to fromPanel widget
	 */
	protected abstract void createComponents();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.extjs.gxt.ui.client.widget.Window#onRender(com.google.gwt.user.client
	 * .Element, int)
	 */
	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		createComponents();
		add(form);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.views.Display#showInfo(java.lang.String)
	 */
	@Override
	public void showInfo(String msg) {
		InfoConfig config = new InfoConfig("Info", msg);
		Info info = new Info();
		info.setAnimCollapse(true);
		info.setBodyStyleName("info-style");
		info.show(config);

		int left = info.getAbsoluteLeft() - 15;
		int top = info.getAbsoluteTop() - 15;
		info.setPosition(left, top);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.Display#setPresenter(com.ingenius.sau
	 * .client.mvp.presenters.Presenter)
	 */
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = (EditPresenter) presenter;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.views.Display#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.extjs.gxt.ui.client.widget.Window#show()
	 */
	@Override
	public void show() {
		if (!this.isVisible()) {
			super.show();
			this.el().slideIn(Direction.DOWN, FxConfig.NONE);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.extjs.gxt.ui.client.widget.Window#hide()
	 */
	@Override
	public void hide() {
		if (this.isVisible()) {
			this.el().slideOut(Direction.UP, FxConfig.NONE);
			super.hide();
		}
	}

}
